/**
 * pages模版快速生成脚本,执行命令 npm run tep `文件名`
 */

import { mkdirSync, writeFileSync } from 'fs';

const dirName = process.argv[2];

if (!dirName) {
    console.log('文件夹名称不能为空！');
    console.log('示例：npm run tep test');
    process.exit(0);
}

// 页面模版
const indexTep = `
import React, { useState, useEffect } from 'react';
import { View } from '@tarojs/components';
import { list } from '@/chaos/functions/Data';
import { getParams } from '@/chaos/functions/Taro';
import { doLogin } from '@/app/utils/login';
import styles from './index.module.scss';

const Index: React.FC = () => {
    const [listData, setListData] = useState<[]>();
    const load = async () => {
        await doLogin(getParams(), true);
    };
    useEffect(() => {
        load();
    }, []);
     

    return (
        <View className={styles.page}>
            <View>Hello My Dear!</View>
        </View>
    );
};

export default Index;

`;

// scss文件模版
const scssTep = `
.page {
}
`;

mkdirSync(`./src/pages/${dirName}`); // mkdir $1
process.chdir(`./src/pages/${dirName}`); // cd $1

writeFileSync('index.tsx', indexTep);
writeFileSync('index.module.scss', scssTep);

console.log(`模版${dirName}已创建,请手动增加models`);

function titleCase(str) {
    const array = str.toLowerCase().split(' ');
    for (let i = 0; i < array.length; i++) {
        array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
    }
    const string = array.join(' ');
    return string;
}

process.exit(0);
